package cn.liuhuan1.blogsModel.service;

import cn.liuhuan1.blogsModel.dao.ArticleDao;
import cn.liuhuan1.entity.PageResult;
import cn.liuhuan1.entity.ReturnResult;
import cn.liuhuan1.webModelBean.api.ArticleImp;
import cn.liuhuan1.webModelBean.bo.ArticleBo;
import cn.liuhuan1.webModelBean.vo.ArticleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ArticleService implements ArticleImp {
    @Autowired
    public ArticleDao articleDao;

    @Override
    public List<ArticleVo> getArticle(){
        List<ArticleVo> articleVoList = articleDao.getArticle();
        return articleVoList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageResult<ArticleVo> getPageArticle(ArticleBo articleBo) {
        if(articleBo.getPageCurrent() < 1){
            articleBo.setPageCurrent(0);
        }else{
            articleBo.setPageCurrent(articleBo.getPageCurrent() - 1);
        }
        if(articleBo.getPageSize() < 9){
            articleBo.setPageCurrent(10);
        }

        PageResult<ArticleVo> pageResult = new PageResult<>();

        List<ArticleVo> webListVoList = articleDao.getPageArticle(articleBo);

        int count = articleDao.getPageCount(articleBo);

        pageResult.setPageList(webListVoList);

        pageResult.setPageShowTotal(count);
        pageResult.setPageCurrent(articleBo.getPageCurrent());
        pageResult.setPageSize(articleBo.getPageSize());
        return pageResult;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ArticleVo getArticleById(String id) {
        if(id != null && id.trim() != ""){
            ArticleVo articleVo = articleDao.getArticleById(id);
            if(articleVo != null){
                articleDao.updateViewNumber(id);
                return articleVo;
            }
        }
        throw new NullPointerException();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveArticle(ArticleBo articleBo) {
        if(null == articleBo.getRecommendStatus()){
            articleBo.setRecommendStatus(2);
        }
        if(null == articleBo.getCategoryId() || "".equals(articleBo.getCategoryId())){
            articleBo.setCategoryId("1");
        }
        articleBo.setPraiseSumber(1);
        articleBo.setReplyNumber(1);
        articleBo.setViewSumber(1);
            articleDao.saveArticle(articleBo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateArticle(ArticleBo articleBo) {

            articleDao.updateArticle(articleBo);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delArticle(String id) {
        if(id != null && id.trim() != ""){
            articleDao.delArticle(id);
        }else{
            throw new NullPointerException();
        }
    }

    @Override
    public void updateStatus(String status,String id) {
        if(status == null || status.trim() == ""){
            throw  new NullPointerException();
        }
        articleDao.updateStatus(status,id);
    }

    @Override
    public List<ArticleVo> getInId(List<String> idList) {
        return articleDao.getInId(idList);
    }

    @Override
    public ReturnResult updateViewSumber(String id) {
        articleDao.updateViewNumber(id);
        return ReturnResult.succes();
    }

    @Override
    public ReturnResult updateReplyNumber(String id) {
        articleDao.updateReplyNumber(id);
        return ReturnResult.succes();
    }

    @Override
    public ReturnResult updatePraiseSumber(String id) {
        articleDao.updatePraiseSumber(id);
        return ReturnResult.succes();
    }


}
